#-*- coding: utf-8 -*-
import tensorflow as tf
from model.embedding_matching_net import EMNet
from utils import data_util
from utils import config

conf = config.Config
# model parameter
EPOCH = conf.epoch
BATCH_SIZE = conf.batch_size
LEARNING_RATE = conf.learning_rate
NUM_SAMPLE = conf.num_sample
EMBEDDING_SIZE = conf.embedding_size
ATTENTION_SIZE = conf.attention_size

# data parameter
ITEM_INPUT_LENGTH = conf.item_input_length
OTHER_INPUT_LENGTH = conf.other_input_length
PATH_TFRECORD_TRAIN = conf.path_tfrecord_train
PATH_TFRECORD_VALIDATION = conf.path_tfrecord_validation
PATH_DICT = conf.path_dict
PATH_MODEL = conf.path_model


def main():
    item_dict = data_util.get_dict(PATH_DICT)
    print("record load finished")
    bea_model = EMNet(len(item_dict), EMBEDDING_SIZE, NUM_SAMPLE, LEARNING_RATE, ATTENTION_SIZE, ITEM_INPUT_LENGTH, OTHER_INPUT_LENGTH)

    with tf.Session() as sess:
        # load model
        saver = tf.train.Saver()
        saver.restore(sess, PATH_MODEL)

        similar_item_input = tf.placeholder(tf.int64, shape=[None])
        # L2 Norm
        vec_l2_model = tf.sqrt(tf.reduce_sum(tf.square(bea_model.nce_weight), 1, keep_dims=True))

        normed_embedding = bea_model.nce_weight / vec_l2_model

        input_item_embed = tf.nn.embedding_lookup(normed_embedding, similar_item_input)

        similar_result = tf.matmul(input_item_embed, normed_embedding, transpose_b=True)

        # top k value and index
        top_value, top_index = tf.nn.top_k(similar_result, k=5, sorted=True)

        feed_dict = {
            similar_item_input: data_util.find_value_by_key(item_dict, ['o06556sg3wh', '7447398155765986502', '7447398156090463203'])
        }
        similar, similar_value, similar_index = sess.run([similar_result, top_value, top_index], feed_dict)
        similar_item = data_util.find_key_by_value(item_dict, similar_index)
        print("similar: {} \nsimilar_value:{} \nsimilar_index:{} \nsimilar_item:{}".format(similar, similar_value, similar_index, similar_item))

if __name__ == '__main__':
    main()





